package com.qf.springboot.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("t_users")
public class User implements Serializable {
    private Integer id;
    @NotNull
    @NotEmpty
    @Size(min = 2, max = 10)
    private String name;
    private String password;
    private Integer sex;
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date birthday;
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @TableField("registTime")
    private Date registTime;
    private String auth;
    private String photopath;

    public String toCSVString() {
        String line = this.getId() + ","
                + this.getName() + ","
                + this.getPassword() + ","
                + this.getSex() + ","
                + this.getBirthday() + ","
                + this.getRegistTime() + ","
                + this.getAuth() + ","
                + this.getPhotopath() + "\n";
        return line;
    }

    public static User cvsToUser(String str) {
        User user = new User();
        if (str != null && str != null) {
            String[] userArr = str.split(",");
            user.setId(Integer.valueOf(userArr[0]));
            user.setName(userArr[1]);
            user.setPassword(userArr[2]);
            user.setSex(Integer.valueOf(userArr[3]));
            user.setBirthday(new Date(userArr[4]));
            user.setRegistTime(new Date(userArr[5]));
            user.setAuth(userArr[6]);
            user.setPhotopath(userArr[7]);
        }
        return user;
    }
}
